<?php
	require_once ("../../config.php");

	# Verify what button was clicked.
	$btaction = $_POST["submit_ccfind"];

	if ($btaction == "Voltar"){
		header("Location: /itcontrol/com/main/main.php");
	}else{	
		
		# Get data previous inserted
		$nameCC = $_POST["input_nameCC"];
		$cc = $_POST["input_cc"];
		$unit = $_POST["input_nameUnit"];
		
		$fields[0] = "nameCC";
		$fields[1] = "cc";
		$fields[2] = "unit_id";
		
		$namefields[0] = "Nome";
		$namefields[1] = "CC";
		$namefields[2] = "Unidade";
		
		$dbconnection = new connectDB();
			
		$count = 0;

		# Check if there is some empty field
		if(!empty($nameCC)){	
			$where[$count] = "tb_cc." . $fields[0]. "='" .$nameCC. "'";
			$count = $count + 1;
		}
		
		if(!empty($cc)){
			$where[$count] = "tb_cc." . $fields[1]. "='" .$cc. "'";
			$count = $count + 1;
		}
		
		if(!empty($unit)){
			
			$fieldsOpt[0] = "id";
			
			$namefieldsOpt[0] = "ID";
			
			$whereOpt[0] = "nameUnit = '" .$unit. "'";
			
			$query = mysql_query($dbconnection->selectDB("tb_unit", "unit", "find", $fieldsOpt, $namefieldsOpt, $whereOpt));
			$result = mysql_fetch_array($query);
			
			$unit = $result[0];
			
			$where[$count] = "tb_cc." . $fields[2]. "='" .$unit. "'";
			$count = $count + 1;
		}
		
		$query = mysql_query($dbconnection->selectDB("tb_cc", "cc", "find", $fields, $namefields, $where));
		
		$query = mysql_fetch_array($query);
		
		$values = urlencode(implode(",", $where));
		
		$dbconnection->disconnectDB();
		
		header("Location: /itcontrol/com/main/cc/cc_list.php?return=" .$values);
	}
?>